﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using ZLCMS.Web.Wap.Code;
using System.Collections;
using NHibernate.Transform;

namespace ZLCMS.Web.Wap.Shop
{
    public partial class Pinbuying : MemberPageBase
    {
        protected ShopPinbuyingConfig pbConfig { get; set; }

        protected ShopPinbuyingInfo pbMain { get; set; }

        protected ShopPinbuyingOrder pbOrder { get; set; }

        protected IDictionary pbTeamLeader { get; set; }

        protected JObject ProductData { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            string pbPorductNo = "";
            using(ISession session = SessionFactoryHelper.CreateSession())
            {
                pbConfig = session.Get<ShopPinbuyingConfig>(Int32.Parse(Request["pbc"]));
                if (pbConfig == null)
                {
                    Response.Write("参数错误");
                    Response.End();
                    return;
                } 
                else if (pbConfig.StartDate > DateTime.Now)
                {
                    Response.Write("活动未开始");
                    Response.End();
                    return;
                }
                else if (pbConfig.EndDate < DateTime.Now)
                {
                    Response.Write("活动已过期");
                    Response.End();
                    return;
                }
                else if(pbConfig.Status != "1")
                {
                    Response.Write("状态异常");
                    Response.End();
                    return;
                }

                int pbi = 0;
                Int32.TryParse(Request["pbi"], out pbi);
                if(pbi > 0)
                {
                    pbMain = session.Get<ShopPinbuyingInfo>(pbi);
                    if (TeamExisted && pbMain.Status == "0")
                    {
                        Response.Write("当前团信息状态为更新，请稍候再尝试...");
                        Response.End();
                        return;
                    }
                }

                if (TeamExisted)
                {
                    pbOrder = session.CreateQuery("from ShopPinbuyingOrder where MainId=:id and MemberAccount=:acc")
                        .SetParameter("id", pbMain.Id)
                        .SetParameter("acc", CurrentMemberAccount)
                        .SetMaxResults(1)
                        .UniqueResult<ShopPinbuyingOrder>();
                    if (pbOrder != null)
                    {
                        pbPorductNo = session.CreateSQLQuery("select product_no from shop_order_product where order_no=:no")
                            .SetParameter("no", pbOrder.OrderNo)
                            .UniqueResult<string>();
                    }
                    IList<IDictionary> list = session.CreateSQLQuery(@"select po.CREATE_DATE,po.MEMBER_ACCOUNT,m.MEMBER_NAME,m.PHOTO_URL from shop_pinbuying_order po INNER JOIN member_info m on po.MEMBER_ACCOUNT=m.ACCOUNT_ID and po.STATUS='1' where MAIN_ID=:mid
    order by po.RECID")
                        .SetParameter("mid", pbMain.Id)
                        .SetResultTransformer(Transformers.AliasToEntityMap)
                        .List<IDictionary>();
                    RepeaterMembers.DataSource = list;
                    RepeaterMembers.DataBind();

                    pbTeamLeader = list[0];
                    list.Remove(pbTeamLeader);
                    RepeaterTeam.DataSource = list;
                    RepeaterTeam.DataBind();
                }
            }

            IList<JObject> plist = JsonConvert.DeserializeObject<List<JObject>>(pbConfig.ProductJson);
            if (!string.IsNullOrEmpty(pbPorductNo))
            {
                foreach (JObject p in plist)
                {
                    if(p["ProductNo"].Value<string>() == pbPorductNo)
                    {
                        ProductData = p;
                        break;
                    }
                }
            }
            RepeaterProduct.DataSource = plist; 
            RepeaterProduct.DataBind();
        }

        protected object GetProductProp(object item, string prop)
        {
            Newtonsoft.Json.Linq.JObject jItem = item as Newtonsoft.Json.Linq.JObject;
            return jItem[prop].Value<string>();
        }

        protected bool TeamExisted { get { return pbMain != null; } }

        //是否允许参与拼团
        protected bool JoinEnable { get { return pbMain != null && pbMain.Status == "1"; } }
    }
}